Skip to content

feat: add SQLCommenter observability to database drivers (fixes #2899)#2913

Closed
tanujbolisetty wants to merge 7 commits into
googleapis:mainfrom
tanujbolisetty:feat/sqlcommenter-integration
Closed

feat: add SQLCommenter observability to database drivers (fixes #2899)#2913
tanujbolisetty wants to merge 7 commits into
googleapis:mainfrom
tanujbolisetty:feat/sqlcommenter-integration

Conversation

@tanujbolisetty
Copy link
Copy Markdown

🚀 Overview

This PR introduces SQLCommenter observability across all supported database drivers in the GenAI Toolbox. It automatically injects rich contextual metadata (tool name, framework, agent controller, database driver, and OpenTelemetry traceparent) into every query executed by the toolbox.

By standardizing this telemetry pattern, downstream database administrators and cloud monitoring tools (like Cloud SQL Insights) can seamlessly map database loads back to specific AI agents and MCP tools without any manual parsing.

🛠 Features & Modifications

1. Comprehensive Driver Coverage

We have instrumented the RunSQL method on 100% of SQL-capable database sources.
The metadata is injected safely as a block comment (/* ... */) before the trailing semicolon (where applicable) to preserve syntax parsing.

  • Instrumented Standard SQL Drivers: alloydbpg, clickhouse, cockroachdb, cloudsqlmssql, cloudsqlmysql, cloudsqlpg, firebird, mindsdb, mssql, mysql, oceanbase, oracle, postgres, singlestore, snowflake, spanner, sqlite, tidb, trino, yugabytedb.
  • Instrumented Query/Graph Drivers: cassandra (CQL) and couchbase (N1QL). For Couchbase, the Source RunSQL signature and corresponding Tool wrapper Invoke interface were refactored to explicitly accept context.Context to enable propagation.
  • Native Labeling: bigquery is instrumented via native BigQuery Job Labels instead of SQL Comments, leveraging the same contextual map.
  • Exclusions (Safety): Object/NoSQL stores (mongodb, firestore, redis, bigtable, dgraph) were explicitly skipped as they do not support standard SQL comment blocks, preventing catastrophic parsing crashes.

2. Context Injection Pipeline

  • Created internal/sqlcommenter/commenter.go to handle context reading, deterministic key sorting, formatting (RFC 3986 URL encoding), and OpenTelemetry span extraction.

  • MCP Auto-Context Pipeline: internal/server/server.go was updated to trap the clientInfo.name payload from the MCP initialize handshake in a concurrent mcpClientNames sync map. api.go extracts this dynamically during tool invocation to populate the controller field automatically (e.g. associating queries directly with "Mosa" or "Claude Desktop").

  • Fallback configurations TOOLBOX_APP_NAME and TOOLBOX_AGENT_NAME were established to manually override the context if needed.

3. Core Framework & Build Architecture Upgrades

These changes resolve significant localized testing and binary compilation blockers encountered during development:

  • API UI Autostart: Modified internal/server/server.go configuration booting logic so that passing the --ui config implicitly turns on the REST API endpoints (if cfg.EnableAPI || cfg.UI).
  • Oracle CGO Decoupling: Separated the Oracle driver implementations into two new files (oci_cgo.go and oci_pure.go). By leveraging Go build tags (//go:build cgo), the toolbox now dynamically compiles using the lightweight go-ora/v2 Pure Go driver by default, entirely sidestepping notoriously complex godror C compiler dependencies native to Oracle cross-compilation.

🧪 Verification

  • Full test matrix suite completely passing: go test ./internal/sqlcommenter/....
  • Tested the append function string manipulation algorithm safely by mocking the WithDBDriver and WithToolName context decorators directly, avoiding the need for live mocked database instances in CI.
  • go build ./... successfully compiles across pure GO targets natively.

📝 Documentation

  • Overhauled docs/SQLCOMMENTER_README.md and explicitly mapped out the full driver support matrix, environment overrides, and NoSQL exclusions.

  • Recommendation: Ensure DEVELOPER.md or the primary README.md is updated to highlight that the default codebase cross-compiles with Oracle's PureGo driver to unblock new developers!

@tanujbolisetty tanujbolisetty requested review from a team as code owners March 31, 2026 19:28
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Mar 31, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements the SQLCommenter observability layer across all SQL-capable database drivers, enabling query tracking by tool name, agent identity, and trace metadata. It introduces the internal/sqlcommenter package and updates the MCP server to persist agent names across sessions. Feedback identifies a memory leak in the session name map, suggests refactoring duplicated RunSQL logic in the CockroachDB source, and recommends using %20 for space encoding to ensure strict adherence to the SQLCommenter specification.

Comment thread internal/server/server.go
Comment thread internal/sources/cockroachdb/cockroachdb.go
Comment thread internal/sqlcommenter/commenter.go
@tanujbolisetty tanujbolisetty force-pushed the feat/sqlcommenter-integration branch 2 times, most recently from 9836bd1 to b24f64f Compare March 31, 2026 19:35
@tanujbolisetty tanujbolisetty force-pushed the feat/sqlcommenter-integration branch from 3760269 to 09dcc7f Compare April 10, 2026 14:47
@tanujbolisetty tanujbolisetty requested review from a team as code owners April 10, 2026 14:47
@drstrangelooker
Copy link
Copy Markdown
Contributor

Can you please integrate the latest changes on main so we can see what is actually changing?

@tanujbolisetty tanujbolisetty force-pushed the feat/sqlcommenter-integration branch from 90dbf31 to 4102ec5 Compare April 10, 2026 16:18
@tanujbolisetty
Copy link
Copy Markdown
Author

"I've just successfully rebased onto the latest upstream/main (v1.0.0 mcp-toolbox namespace) and squashed the history. The PR is now completely up-to-date with a clean commit trail!"

@averikitsch averikitsch assigned Yuan325 and unassigned duwenxin99 Apr 13, 2026
Copy link
Copy Markdown
Contributor

@Yuan325 Yuan325 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tanujbolisetty Thank you for your contributions! Please let me know if you need any clarifications. :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes should be reversed. We did not update the llamaindex sdk repo name~

Comment thread docs/SPANNER_README.md
Comment on lines +41 to +43
- "Execute a DML query to update customer names."
- "List all tables in the `my-database`."
- "Execute a DQL query to select data from `orders` table."
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep to * to be consistent with other readmes.

Comment thread docs/SPANNER_README.md
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes in this file should be reversed. It is redundant for this sqlcommenter.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doc should not be included here. We can add sqlcommenter under docs/en/documentation/monitoring. Please modify this file to fit with the docs template format. Alternatively, since every source's sqlcommenter is different, we can also add it under each docs/en/integrations source's docs.

| **CloudSQL Postgres** | `internal/sources/cloudsqlpg/cloud_sql_pg.go` | SQL Comment (`/* ... */`) |
| **CockroachDB** | `internal/sources/cockroachdb/cockroachdb.go` | SQL Comment (`/* ... */`) |
| **Couchbase (N1QL)** | `internal/sources/couchbase/couchbase.go`<br>`internal/tools/couchbase/couchbase.go` | N1QL Comment (`/* ... */`) |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove this file. All docs should go under docs. We can concatenate the information here along with the other sqlcommenter documentation.

Comment on lines +287 to +290
// Embed the tool name in the context so RunSQL can attach it as a BigQuery
// Job Label for Cloud SQL Insights / BigQuery observability.
ctx = sqlcommenter.WithToolName(ctx, t.Name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be added in server/mcp?

Comment thread README.md
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reverse, this change is redundant~

Comment on lines +118 to +120
func (s *Source) RunSQL(ctx context.Context, statement string, params parameters.ParamValues, isQuery bool, timeout string) (any, error) {
// Dgraph does not support SQL block comments, so we explicitly skip injecting SQLCommenter metadata here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we are not using ctx here, please reverse this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant, please reverse~

@tanujbolisetty tanujbolisetty deleted the feat/sqlcommenter-integration branch April 26, 2026 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants